﻿using System;

namespace iStudio.Structure.Message
{
    public abstract class MessageClient : IObserver<Message>
    {
        private IDisposable _unsubscriber;

        public abstract void OnNext (Message value);

        public abstract void OnError (Exception error);

        public void Subscribe (IObservable<Message> provider)
        {
            if (provider != null)
            {
                _unsubscriber = provider.Subscribe (this);
            }
        }

        public void OnCompleted ()
        {
            Unsubscribe ();
        }

        private void Unsubscribe ()
        {
            _unsubscriber.Dispose ();
        }
    }
}
